import { useChatDataStore } from "@/store";

// 聊天控制相关
export function useChatControls() {
  const chatDataStore = useChatDataStore();
  // 是否暂停
  const isPause = ref(false);
  // 是否请求加载
  const isRequestLoading = ref(false);
  const abortController = ref<AbortController | null>(null);

  // 是否生成中
  const isGenerating = computed(() =>
    chatDataStore.chatHistoryWin.some((msg) => msg.isGenerating)
  );

  // 暂停生成
  const pauseGeneration = () => {
    isPause.value = true;
    if (abortController.value) {
      abortController.value.abort();
      abortController.value = null;
    }
    // 注掉此代码是为了让 “继续生成” 按钮出现
    // chatDataStore.generatingMessageId = null;
    const generatingIndex = chatDataStore.chatHistoryWin.findIndex(
      (msg) => msg.isGenerating
    );
    if (generatingIndex !== -1) {
      chatDataStore.chatHistoryWin[generatingIndex].isGenerating = false;
      chatDataStore.chatHistoryWin[generatingIndex].isLoading = false;
    }
  };

  return {
    isPause,
    isRequestLoading,
    isGenerating,
    pauseGeneration,
  };
}
